package com.iwdnb.gkgz.ui.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Executor;
import java.util.stream.Collectors;

import cn.hutool.core.date.DateUtil;
import com.google.common.collect.Lists;
import com.iwdnb.bmnf.common.model.result.Result;
import com.iwdnb.bmnf.common.service.CacheService;
import com.iwdnb.gkgz.application.service.HistoryDataService;
import com.iwdnb.gkgz.application.service.RealtimeStockService;
import com.iwdnb.gkgz.application.service.StockDayDataSerivce;
import com.iwdnb.gkgz.application.service.StockPoolService;
import com.iwdnb.gkgz.application.service.StockService;
import com.iwdnb.gkgz.common.model.dto.Stock;
import com.iwdnb.gkgz.common.model.dto.StockDayData;
import com.iwdnb.gkgz.application.model.vo.StockType;
import com.iwdnb.gkgz.common.utils.StockBlackListUtils;
import com.iwdnb.gkgz.common.utils.StockUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 股票历史数据处理
 *
 * @author wbc
 * @date 2020/05/03
 **/
@RestController
@RequestMapping("/api/stock/data")
@Slf4j
public class StockDataController {

    @Autowired
    private Executor executor;
    @Autowired
    HistoryDataService historyDataService;
    @Autowired
    private StockDayDataSerivce stockDayDataSerivce;
    @Autowired
    private RealtimeStockService realtimeStockService;
    @Autowired
    private StockService stockService;
    @Autowired
    private CacheService cacheService;

    @RequestMapping("/loadStockList")
    public Result loadStockList() {
        historyDataService.loadStockDataList(StockType.SZ);
        historyDataService.loadStockDataList(StockType.SH);
        return Result.getSuccessResult(null);
    }

    /**
     * 加载股票指定年份数据
     *
     * @param code
     * @return
     */
    @RequestMapping("/loadStockDayData")
    public Result<String> loadStockDayData(String beginDate, String endDate,
        @RequestParam("code") String code) {
        List<String> codes = Lists.newArrayList(code.split(","));
        for (String c : codes) {
            executor.execute(() -> {
                stockDayDataSerivce.loadStockDayData(c, beginDate, endDate);
            });
        }
        return Result.getSuccessResult(null);
    }

    /**
     * 加载股票指定年份数据
     *
     * @return
     */
    @RequestMapping("/loadAllStockDayData")
    public Result<String> loadAllStockDayData(@RequestParam(defaultValue = "2014-01-01") String beginDate,
        boolean loadFenshiFlag) {
        String endDate = DateUtil.formatDate(new Date());
        stockDayDataSerivce.loadAllStockDayData(beginDate, endDate, loadFenshiFlag, true);
        return Result.getSuccessResult(null);
    }

    /**
     * 同步股票今天实时数据
     *
     * @return
     */
    @RequestMapping("/syncRealtimeData")
    public Result<String> syncRealtimeData() {
        realtimeStockService.syncRealtimeData(null);
        return Result.getSuccessResult(null);
    }

    /**
     * 持久化股票实时数据到数据库,现在用于屠龙刀
     *
     * @return
     */
    @RequestMapping("/storeRealtimeDataToDb")
    public Result<String> storeRealtimeDataToDb() {
        realtimeStockService.storeRealtimeDataToDb(null);
        return Result.getSuccessResult(null);
    }

    /**
     * 加载股票全部涨停的数据，从stockDayData中获取
     *
     * @return
     */
    @RequestMapping("/loadAllLimitUpData")
    public Result<String> loadAllLimitUpData(@RequestParam(defaultValue = "2014-01-01", required = false) String date) {
        stockDayDataSerivce.loadAllLimitUpData(date);
        return Result.getSuccessResult(null);
    }

    /**
     * 查询股票K线数据,不输入年份即为全部
     *
     * @param year
     * @param code
     * @return
     */
    @RequestMapping("/queryStockDayDataList")
    public Result<List<StockDayData>> queryStockDayDataList(String code, String year) {
        List<StockDayData> dayData = historyDataService.queryStockDayDataList(code, year);
        return Result.getSuccessResult(dayData);
    }

    @RequestMapping("/day/query")
    public Result<List<StockDayData>> queryData(@RequestParam("startDate") String startDateStr,
        @RequestParam(value = "endDate", required = false) String endDateStr,
        @RequestParam("code") String code) {
        Date startDate = DateUtil.parseDate(startDateStr);

        Date endDate = new Date();
        if (!StringUtils.isEmpty(endDateStr)) {
            endDate = DateUtil.parseDate(endDateStr);
        }

        List<StockDayData> dayData = historyDataService.getDataByRange(code, startDate, endDate);
        return Result.getSuccessResult(dayData);
    }

    private void loadStockData() {

    }
    //    @RequestMapping("/day/calc")
    //    public Result<List<StockDayDataResultDO>> calcData(@RequestParam("startDate") String startDateStr,
    //                                                @RequestParam(value = "endDate", required = false) String
    //                                                endDateStr,
    //                                                @RequestParam("code") String code){
    //        Date startDate = DateUtil.string2Date(startDateStr,"yyyy-MM-dd");
    //
    //        Date endDate = new Date();
    //        if(!StringUtils.isEmpty(endDateStr)){
    //            endDate = DateUtil.string2Date(endDateStr,"yyyy-MM-dd");
    //        }
    //
    //        List<StockDayDataResultDO> dayData = historyDataService.calcDataByRange(code, startDate, endDate);
    //        return Result.getSuccessResult(dayData);
    //    }

}
